package com.ym.controller.financialManagement;

import com.github.pagehelper.PageInfo;

import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.utils.excel.ExportExcelUtil;

import com.ym.medical.domain.param.ConsumeDetailsParam;
import com.ym.medical.domain.param.TransactionDetailsParam;
import com.ym.medical.domain.vo.ConsumeDetailsVo;
import com.ym.medical.domain.vo.TransactionDetailsVo;
import com.ym.medical.enums.CashFlowEnum;
import com.ym.medical.enums.TransactionDetailsTdTypeEnum;
import com.ym.medical.service.IConsumeDetailsSV;
import com.ym.medical.service.ITransactionDetailsSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 患者账单管理
 */

@Api(tags = "患者账单管理")
@RestController
@RequestMapping("/financialManagement/patient")
@Validated
public class PatientBillManagement extends BaseController {

	@Autowired
	private IConsumeDetailsSV consumeDetailsSV;

	@Autowired
	private ITransactionDetailsSV transactionDetailsSV;

	@ApiOperation("患者消费退费记录 （cashFlow 01-收入（退费） 02-支出）")
	@GetMapping(value = "/consumeDetail/list")
	public APIResponse<PageInfo<ConsumeDetailsVo>> listPatientConsumeDetail(
			ConsumeDetailsParam consumeDetailsParam,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {
		PageInfo<ConsumeDetailsVo> consumeDetailsModelPageInfo = consumeDetailsSV.listPatientConsumeDetail(pageNo, limit, consumeDetailsParam);
		return successData(consumeDetailsModelPageInfo);
	}

	@ApiOperation("患者充值提现记录")
	@GetMapping(value = "/transactionDetail/list")
	public APIResponse<PageInfo<TransactionDetailsVo>> listTransactionDetail(
			TransactionDetailsParam transactionDetailsParam,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {
		PageInfo<TransactionDetailsVo> transactionDetailsModelPageInfo = transactionDetailsSV.listTransactionDetail(transactionDetailsParam, pageNo, limit);
		return successData(transactionDetailsModelPageInfo);
	}

	@ApiOperation("患者消费记录导出")
	@PostMapping("/exportPatientConsume")
	public void exportPatientConsume(HttpServletResponse response,
                                     @RequestBody ConsumeDetailsParam consumeDetailsParam,
                                     @RequestParam(defaultValue = "1") int pageNo,
                                     @RequestParam(defaultValue = "10") int limit) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "患者消费记录";
		String[] headerString = {"序号", "交易单号", "订单编号", "姓名", "手机号", "证件号码", "付款方式", "付款账户", "交易金额", "交易后账户金额", "问诊医生", "交易时间"};
		String[] headerField = {"id", "tdNo", "tradeNo", "userName", "phoneNo", "cardNo", "payTypeName", "payAcc", "tdAmount", "aftTdAmount", "doctorName", "tdTime"};

		consumeDetailsParam.setCashFlow(CashFlowEnum.EXPEND.getKey());
		PageInfo<ConsumeDetailsVo> consumeDetailsModelPageInfo = consumeDetailsSV.listPatientConsumeDetail(pageNo, limit, consumeDetailsParam);
		List<ConsumeDetailsVo> list = consumeDetailsModelPageInfo.getList();
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, list, response, null);
	}

	@ApiOperation("患者退费记录导出")
	@PostMapping("/exportPatientRefund")
	public void exportPatientRefund(HttpServletResponse response,
                                    @RequestBody ConsumeDetailsParam consumeDetailsParam,
                                    @RequestParam(defaultValue = "1") int pageNo,
                                    @RequestParam(defaultValue = "10") int limit) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "患者退费记录";
		String[] headerString = {"序号", "交易单号", "订单编号", "姓名", "手机号", "证件号码", "退款方式", "退款账户", "交易金额", "交易后账户金额", "问诊医生", "交易时间"};
		String[] headerField = {"id", "tdNo", "tradeNo", "userName", "phoneNo", "cardNo", "payTypeName", "payAcc", "tdAmount", "aftTdAmount", "doctorName", "tdTime"};

		consumeDetailsParam.setCashFlow(CashFlowEnum.INCOME.getKey());
		PageInfo<ConsumeDetailsVo> consumeDetailsModelPageInfo = consumeDetailsSV.listPatientConsumeDetail(pageNo, limit, consumeDetailsParam);
		List<ConsumeDetailsVo> list = consumeDetailsModelPageInfo.getList();
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, list, response, null);
	}


	@ApiOperation("患者充值记录导出")
	@PostMapping("/exportPatientRecharges")
	public void exportPatientRecharges(HttpServletResponse response,
                                       @RequestBody TransactionDetailsParam transactionDetailsParam,
                                       @RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "10") int limit) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "患者充值记录";
		String[] headerString = {"序号", "交易单号", "姓名", "手机号", "证件号码", "交易金额", "交易后账户金额", "支付方式", "支付账户", "交易时间"};
		String[] headerField = {"id", "tdNo", "userName", "phoneNo", "cardNo", "tdAmount", "aftTdAmount", "dfAccTypeName", "dfAcc", "tdTime"};

		transactionDetailsParam.setTdType(TransactionDetailsTdTypeEnum.RECHARGE.getKey());
		PageInfo<TransactionDetailsVo> transactionDetailsModelPageInfo = transactionDetailsSV.listTransactionDetail(transactionDetailsParam, pageNo, limit);
		List<TransactionDetailsVo> list = transactionDetailsModelPageInfo.getList();
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, list, response, null);
	}

	@ApiOperation("患者提现记录导出")
	@PostMapping("/exportPatientWithdraw")
	public void exportPatientWithdraw(HttpServletResponse response,
                                      @RequestBody TransactionDetailsParam transactionDetailsParam,
                                      @RequestParam(defaultValue = "1") int pageNo,
                                      @RequestParam(defaultValue = "10") int limit) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "患者提现记录";
		String[] headerString = {"序号", "交易单号", "姓名", "手机号", "证件号码", "交易金额", "交易后账户金额", "支付方式", "支付账户", "交易时间"};
		String[] headerField = {"id", "tdNo", "userName", "phoneNo", "cardNo", "tdAmount", "aftTdAmount", "dfAccTypeName", "dfAcc", "tdTime"};

		transactionDetailsParam.setTdType(TransactionDetailsTdTypeEnum.EMBODY.getKey());
		PageInfo<TransactionDetailsVo> transactionDetailsModelPageInfo = transactionDetailsSV.listTransactionDetail(transactionDetailsParam, pageNo, limit);
		List<TransactionDetailsVo> list = transactionDetailsModelPageInfo.getList();
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, list, response, null);
	}


}
